#include "StdAfx.h"

/*
 * Stride has been included in Qmol with the kind permission of 
 * 
 * Dmitrij Frishman, PhD
 * Institute for Bioinformatics
 * GSF - Forschungszentrum f? Umwelt und Gesundheit, GmbH
 * Ingolst?ter Landstra? 1,
 * D-85764 Neuherberg, Germany
 *
 * Telephone: +49-89-3187-4201
 * Fax: +49-89-31873585
 * e-mail: d.frishman@gsf.de
 * WWW: http://mips.gsf.de/mips/staff/frishman/
 *
 * Stride copyright (see http://www.embl-heidelberg.de/stride/stride.html):
 *
 * All rights reserved, whether the whole  or  part  of  the  program  is
 * concerned.  Permission  to use, copy, and modify this software and its
 * documentation is granted for academic use, provided that:
 *
 *
 * i.	this copyright notice appears in all copies of the software  and
 *		related documentation;
 *
 * ii.  the reference given below (Frishman and  Argos,  1995)  must  be
 *		cited  in any publication of scientific results based in part or
 *		completely on the use of the program;
 *
 * iii.  bugs will be reported to the authors.
 *
 * The use of the  software  in  commercial  activities  is  not  allowed
 * without a prior written commercial license agreement.
 * 
 * WARNING: STRIDE is provided "as-is" and without warranty of any  kind,
 * express,  implied  or  otherwise,  including  without  limitation  any
 * warranty of merchantability or fitness for a particular purpose. In no
 * event will the authors be liable for any special, incidental, indirect
 * or consequential damages  of  any  kind,  or  any  damages  whatsoever
 * resulting  from loss of data or profits, whether or not advised of the
 * possibility of damage, and on any theory of liability, arising out  of
 * or in connection with the use or performance of this software.
 * 
 * For calculation of the residue solvent accessible area the program NSC
 * [3,4]   is   used   and   was  kindly  provided  by  Dr.  F.Eisenhaber
 * (EISENHABER@EMBL-HEIDELBERG.DE). Please direct to  him  all  questions
 * concerning specifically accessibility calculations.
 * 
 * Stride References:
 * 
 * 1.	Frishman,D & Argos,P. (1995) Knowledge-based secondary structure
 * 		assignment.  Proteins:  structure, function and genetics, 23,
 * 		566-579.
 * 
 * 2.	Kabsch,W. & Sander,C. (1983)  Dictionary  of  protein  secondary
 * 		structure:    pattern   recognition   of   hydrogen-bonded   and
 * 		geometrical features. Biopolymers, 22: 2577-2637.
 * 
 * 3.	Eisenhaber,  F.  and  Argos,  P.  (1993)  Improved  strategy  in
 * 		analytic  surface calculation for molecular systems: handling of
 * 		singularities and computational efficiency. J. comput. Chem. 14,
 * 		1272-1280.
 * 
 * 4.	Eisenhaber, F., Lijnzaad, P., Argos, P., Sander, C., and Scharf,
 * 		M.  (1995) The DOUBLE cubic lattice method: efficient approaches
 * 		to numerical integration of surface area and volume and  to  dot
 * 		surface contouring of molecular assemblies. J. comput. Chem. 16,
 * 		273-284.
 * 
 * 5.	Bernstein, F.C., Koetzle, T.F.,  Williams,  G.J.,  Meyer,  E.F.,
 * 		Brice,  M.D.,  Rodgers,  J.R., Kennard, O., Shimanouchi, T., and
 * 		Tasumi, M.  (1977)  The  protein  data  bank:  a  computer-based
 * 		archival  file for macromolecular structures. J. Mol. Biol. 112,
 * 		535-542.
 * 
 * 6.	Kraulis, P.J.  (1991)  MOLSCRIPT:  a  program  to  produce  both
 * 		detailed  and  schematic  plots  of protein structures. J. Appl.
 * 		Cryst. 24, 946-950.
 * 
 * 7.	Pearson, W.R. (1990) Rapid  and  sensitive  sequence  comparison
 * 		with FASTP and FASTA. Methods. Enzymol. 183, 63-98.
 * 
 */
#include "stride.h"

INT32 ReadPhiPsiMap(char *MapFile, FLOAT ***PhiPsiMap, COMMAND *Cmd)
{

  INT32 i, j, NFields, Cnt=0;
  FILE *fi;
  BUFFER Buffer;
  char *Fields[MAX_FIELD];

  Cmd->NPixel = 0;

  if( (fi = fopen(MapFile,"r")) != 0 ) {

    while( fgets(Buffer,BUFSZ,fi) != NULL ) {
      if( !(NFields = SplitString(Buffer,Fields,MAX_FIELD)) ) continue;
      if( Cmd->NPixel == 0 ) {
	if( !strcmp(tolostr(Fields[0]),"npixel") ) {
	  Cmd->NPixel = atoi(Fields[1]);
	  if( Cmd->NPixel < 1 || Cmd->NPixel > 1000 ) 
	    die("Wrong number of pixels in the PhiPsi Map file %s\n",MapFile);
	  *PhiPsiMap = FloatMatrix(Cmd->NPixel,Cmd->NPixel);
	}
      }
      else  {
	if( !strcmp(tolostr(Fields[0]),"pixel") ) {
	  if( (i = atoi(Fields[1])) >= 0 && i < Cmd->NPixel &&
	      (j = atoi(Fields[2])) >= 0 && j < Cmd->NPixel &&
	     Cnt ==  i*Cmd->NPixel+j &&  NFields >= 4 ) {
	    (*PhiPsiMap)[i][j] = (FLOAT)(atof(Fields[5]));
	    Cnt++;
	  }
	  else die("Error in the PhiPsi Map file %s\n",MapFile);
	}
      }
    }
    fclose(fi);
    Cmd->PhiPsiStep = (FLOAT)((MAXPHIPSI - MINPHIPSI)/(FLOAT)Cmd->NPixel);
  }
  
  if( !Cmd->NPixel ) die("Error reading PhiPsiMap file %s\n",MapFile);

  return(Cmd->NPixel);
}

FLOAT **DefaultHelixMap(COMMAND *Cmd)
{

  register INT32 i;

  FLOAT **Map;
  static FLOAT Data[DEFNUMPIXEL][DEFNUMPIXEL] = {

      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0009014423f, 0.0041898815f,
      0.0085105160f, 0.0133839026f, 0.0245425366f, 0.0407802090f, 0.0464176536f, 0.0330946408f,
      0.0134803243f, 0.0024038462f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0007370283f, 0.0077203326f, 0.0269849468f,
      0.0492307022f, 0.0621860325f, 0.0747849122f, 0.0919913873f, 0.0918549150f, 0.0617070347f,
      0.0241584498f, 0.0041428790f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0041416897f, 0.0287234355f, 0.0835687742f,
      0.1384727061f, 0.1562444866f, 0.1470608264f, 0.1360232681f, 0.1159155145f, 0.0742164999f,
      0.0290896539f, 0.0050673936f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0009375000f, 0.0156580955f, 0.0757770315f, 0.1856354773f,
      0.2785892785f, 0.2880102694f, 0.2332847565f, 0.1741978228f, 0.1281246394f, 0.0793832615f,
      0.0320557840f, 0.0058840578f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0048893229f, 0.0437000208f, 0.1617751122f, 0.3399706185f,
      0.4626395404f, 0.4418565035f, 0.3235570788f, 0.2100441158f, 0.1358627081f, 0.0776144490f,
      0.0297011137f, 0.0052390974f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0136979166f, 0.0917820632f, 0.2773087323f, 0.5047551394f,
      0.6214492917f, 0.5485223532f, 0.3655386865f, 0.2054343373f, 0.1121114418f, 0.0548815951f,
      0.0178668182f, 0.0025975490f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0246484373f, 0.1396044195f, 0.3594934344f, 0.5710113049f,
      0.6337110400f, 0.5133636594f, 0.3054708838f, 0.1402616948f, 0.0584463216f, 0.0228670351f,
      0.0058531328f, 0.0005151099f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0265885405f, 0.1365883052f, 0.3163702190f, 0.4545661211f,
      0.4628692269f, 0.3425511420f, 0.1761947423f, 0.0607788190f, 0.0158569515f, 0.0042061093f,
      0.0008107311f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0152018229f, 0.0738445148f, 0.1630392224f, 0.2269553691f,
      0.2237145752f, 0.1528334022f, 0.0652616471f, 0.0150429625f, 0.0014589608f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0035156249f, 0.0165251363f, 0.0379281938f, 0.0584417619f,
      0.0619409233f, 0.0404052660f, 0.0136552500f, 0.0016678370f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0011718750f, 0.0046875002f,
      0.0070312503f, 0.0046875002f, 0.0011718750f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0006944445f, 0.0036063762f, 0.0080820229f, 0.0101532144f, 0.0076146079f,
      0.0032324446f, 0.0006009616f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f
  };


  Map = (FLOAT **)ckalloc(DEFNUMPIXEL*sizeof(FLOAT *));

  for( i=0; i<DEFNUMPIXEL; i++ )
    Map[i] = &(Data[i][0]);

  Cmd->NPixel = DEFNUMPIXEL;
  Cmd->PhiPsiStep = (FLOAT)((MAXPHIPSI - MINPHIPSI)/(FLOAT)Cmd->NPixel);

  return(Map);
}


FLOAT **DefaultSheetMap(COMMAND *Cmd)
{

  register INT32 i;
  FLOAT **Map;
  static FLOAT Data[DEFNUMPIXEL][DEFNUMPIXEL] = {

      0.2769023776f, 0.1408346891f, 0.0464910716f, 0.0073784725f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0047086575f, 0.0218229108f, 0.0569166169f,
      0.1254088134f, 0.2340224832f, 0.3511219919f, 0.4355685711f, 0.4584180117f, 0.4007356465f,
      0.4067636132f, 0.2329865396f, 0.0927943364f, 0.0237838365f, 0.0055147060f, 0.0013786765f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0088186050f, 0.0420726910f, 0.1043856740f,
      0.2086037844f, 0.3677131534f, 0.5367187858f, 0.6412357688f, 0.6458424330f, 0.5580080152f,
      0.4286311865f, 0.2678007782f, 0.1282834113f, 0.0529448465f, 0.0220588241f, 0.0055147060f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0086062262f, 0.0445192643f, 0.1197573245f,
      0.2487278134f, 0.4369854629f, 0.6241853237f, 0.7160459757f, 0.6829043031f, 0.5716546178f,
      0.3639202416f, 0.2397334576f, 0.1305907220f, 0.0683420748f, 0.0330882370f, 0.0082720593f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0053559211f, 0.0328565054f, 0.1048930883f,
      0.2402425259f, 0.4295993447f, 0.6026929021f, 0.6669865251f, 0.6039550304f, 0.4841639400f,
      0.2637948096f, 0.1723874062f, 0.0920098722f, 0.0464194641f, 0.0220588241f, 0.0055147060f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0030202419f, 0.0224239044f, 0.0804052502f,
      0.1923188865f, 0.3456886411f, 0.4811576009f, 0.5223571062f, 0.4586051404f, 0.3565762639f,
      0.1628032923f, 0.0930610597f, 0.0400134660f, 0.0143100554f, 0.0055147060f, 0.0013786765f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0015453297f, 0.0132468110f, 0.0489843786f,
      0.1174781919f, 0.2150468081f, 0.3082944453f, 0.3439011276f, 0.3080393970f, 0.2371628135f,
      0.0825822726f, 0.0338854715f, 0.0092895878f, 0.0012122844f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0003863324f, 0.0046614520f, 0.0186656341f,
      0.0477515720f, 0.0961741805f, 0.1546680480f, 0.1961039603f, 0.1944279373f, 0.1469529718f,
      0.0326442868f, 0.0073916214f, 0.0008854167f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0006347656f, 0.0031504754f,
      0.0104655549f, 0.0272454955f, 0.0570511036f, 0.0941907763f, 0.1088592261f, 0.0785619915f,
      0.0090501504f, 0.0007651417f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0007207961f, 0.0035958111f, 0.0131648667f, 0.0318824202f, 0.0425693691f, 0.0292618107f,
      0.0013020834f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0013020834f, 0.0052083335f, 0.0078125000f, 0.0052083335f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0210939310f, 0.0078523019f, 0.0013020834f, 0.0000000000f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0014204546f,
      0.0071634995f, 0.0169352461f, 0.0272206441f, 0.0357281528f, 0.0395361669f, 0.0343801714f,
      0.1146211401f, 0.0503530800f, 0.0130920913f, 0.0015190972f, 0.0000000000f, 0.0000000000f,
      0.0000000000f, 0.0000000000f, 0.0000000000f, 0.0010016026f, 0.0046167620f, 0.0157516468f,
      0.0453012958f, 0.0937970504f, 0.1454590708f, 0.1861637682f, 0.2019522935f, 0.1764564067f
  };

 
  Map = (FLOAT **)ckalloc(DEFNUMPIXEL*sizeof(FLOAT *));

  for( i=0; i<DEFNUMPIXEL; i++ )
    Map[i] = &(Data[i][0]);

  Cmd->NPixel = DEFNUMPIXEL;
  Cmd->PhiPsiStep = (FLOAT)((MAXPHIPSI - MINPHIPSI)/(FLOAT)Cmd->NPixel);

  return(Map); 
}
